package com.kzc.controller;

import com.kzc.base.model.Result;
import com.kzc.entity.Member;
import com.kzc.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * Created by anyleKe on 2016/1/30.
 */
@Controller
public class MemberController {

    @Autowired
    MemberService memberService;

    //用户注册
    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result register(@ModelAttribute Member member, @RequestParam String code) {
//        member.setDevice(request.getHeader("User-Agent"));//记录注册设备信息

        return memberService.register(member, code);
    }


    //用户登录
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestParam String phone, @RequestParam String password) {

        Member member = memberService.login(phone, password);
        if (null != member) {
            return new Result(Result.SUCCESS, member, "登录成功！");
        }
        Member member1 = memberService.queryByPhone(phone);
        if (null == member1) {
            return new Result(Result.ERROR, "用户未注册，请先注册！");
        } else {
            return new Result(Result.ERROR, "密码错误！");
        }
    }

    //重置密码
    @ResponseBody
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public Result resetPassword(@ModelAttribute Member member, @RequestParam String code) {
        return memberService.resetPassword(member, code);
    }

    //绑定E通卡
    @ResponseBody
    @RequestMapping(value = "/bindEcard", method = RequestMethod.POST)
    public Object bindEcard(@RequestParam String e_card, @RequestParam String qtoken) {
        int i = memberService.bindEcard(e_card, qtoken);
        if (i <= 0) {
            return new Result(Result.ERROR, "绑定失败！");
        } else {
            return new Result(Result.SUCCESS, "绑定成功！");
        }
    }

    //绑定宿舍号
    @ResponseBody
    @RequestMapping(value = "/bindDormitoryNo", method = RequestMethod.POST)
    public Object bindDormitoryNo(@RequestParam String dormitoryNo, @RequestParam String qtoken) {
        int i = memberService.bindDormitoryNo(dormitoryNo, qtoken);
        if (i <= 0) {
            return new Result(Result.ERROR, "绑定失败！");
        } else {
            return new Result(Result.SUCCESS, "绑定成功！");
        }
    }


    //绑定教务信息
    @ResponseBody
    @RequestMapping(value = "/bindStudentInfo", method = RequestMethod.POST)
    public Object bindStudentInfo(@RequestParam String student_no, @RequestParam String student_pwd, @RequestParam String qtoken) {
        int i = memberService.bindStudentInfo(student_no, student_pwd, qtoken);
        if (i <= 0) {
            return new Result(Result.ERROR, "绑定失败！");
        } else {
            return new Result(Result.SUCCESS, "绑定成功！");
        }
    }

    //修改性别
    @ResponseBody
    @RequestMapping(value = "/updateGender", method = RequestMethod.POST)
    public Object updateGender(@RequestParam String gender, @RequestParam String qtoken) {
        int i = memberService.updateGender(gender, qtoken);
        if (i <= 0) {
            return new Result(Result.ERROR, "修改失败！");
        } else {
            return new Result(Result.SUCCESS, "修改成功！");
        }
    }

    //修改昵称
    @ResponseBody
    @RequestMapping(value = "/updateNick", method = RequestMethod.POST)
    public Object updateNick(@RequestParam String nickname, @RequestParam String qtoken) {
        int i = memberService.updateNick(nickname, qtoken);
        if (i <= 0) {
            return new Result(Result.ERROR, "修改失败！");
        } else {
            return new Result(Result.SUCCESS, "修改成功！");
        }
    }

//    //修改头像名
//    @ResponseBody
//    @RequestMapping(value = "/updateAvatar", method = RequestMethod.POST)
//    public Object updateAvatar(@RequestParam String avatar, @RequestParam String qtoken) {
//        int i = memberService.updateAvatar(avatar, qtoken);
//        if (i <= 0) {
//            return new Result(Result.ERROR, "修改失败！");
//        } else {
//            return new Result(Result.SUCCESS, "修改成功！");
//        }
//    }

    //修改密码
    @ResponseBody
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public Object updatePassword(@RequestParam String password, @RequestParam String qtoken) {
        Member member = memberService.queryInfo(qtoken);
        if (null == member) {
            return new Result(Result.ERROR, "用户不存在");
        }
        int i = memberService.updatePassword(password, qtoken);
        if (i > 0) {
            return new Result(Result.SUCCESS, member, "修改成功！");
        } else {
            return new Result(Result.ERROR, "修改失败！");
        }
    }

    //获取个人信息
    @ResponseBody
    @RequestMapping(value = "/queryInfo", method = RequestMethod.GET)
    public Object queryInfo(String qtoken) {
        System.out.println(qtoken);
        Member member = memberService.queryInfo(qtoken);
        if (member != null) {
            return new Result(Result.SUCCESS, member, "获取成功！");
        } else {
            return new Result(Result.ERROR, "获取失败！");
        }
    }

    //修改学生姓名
    @ResponseBody
    @RequestMapping(value = "/updateStudentName", method = RequestMethod.POST)
    public Result updateStudentName(@RequestParam String studentName, @RequestParam String qtoken) {
        return memberService.updateStudentName(studentName, qtoken);
    }

    //修改学生专业
    @ResponseBody
    @RequestMapping(value = "/updateMajor", method = RequestMethod.POST)
    public Result updateMajor(@RequestParam String major, @RequestParam String qtoken) {
        return memberService.updateMajor(major, qtoken);
    }
}
